home *** CD-ROM | disk | FTP | other *** search
/ Acorn RISC PD-CD 1 / Acorn RISC PD-CD 1.iso / utilities / _graphics / graphics / _jpeg / h / jpegdata < prev   
Encoding:
Text File  |  1991-10-28  |  38.5 KB  |  813 lines

  1. /*
  2.  * jpegdata.h
  3.  *
  4.  * Copyright (C) 1991, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file defines shared data structures for the various JPEG modules.
  9.  */
  10.  
  11.  
  12. /*
  13.  * You might need to change some of the following declarations if you are
  14.  * using the JPEG software within a surrounding application program
  15.  * or porting it to an unusual system.
  16.  */
  17.  
  18.  
  19. /* If the source or destination of image data is not to be stdio streams,
  20.  * these types may need work.  You can replace them with some kind of
  21.  * pointer or indicator that is useful to you, or just ignore 'em.
  22.  * Note that the user interface and the various jrdxxx/jwrxxx modules
  23.  * will also need work for non-stdio input/output.
  24.  */
  25.  
  26. typedef FILE * JFILEREF;        /* source or dest of JPEG-compressed data */
  27.  
  28. typedef FILE * IFILEREF;        /* source or dest of non-JPEG image data */
  29.  
  30.  
  31. /* These defines are used in all function definitions and extern declarations.
  32.  * You could modify them if you need to change function linkage conventions,
  33.  * as is shown below for use with C++.  Another application would be to make
  34.  * all functions global for use with code profilers that require it.
  35.  * NOTE: the C++ test does the right thing if you are reading this include
  36.  * file in a C++ application to link to JPEG code that's been compiled with a
  37.  * regular C compiler.  I'm not sure it works if you try to compile the JPEG
  38.  * code with C++.
  39.  */
  40.  
  41. #define METHODDEF static        /* a function called through method pointers */
  42. #define LOCAL     static        /* a function used only in its module */
  43. #define GLOBAL                  /* a function referenced thru EXTERNs */
  44. #ifdef __cplusplus
  45. #define EXTERN    extern "C"    /* a reference to a GLOBAL function */
  46. #else
  47. #define EXTERN    extern        /* a reference to a GLOBAL function */
  48. #endif
  49.  
  50.  
  51. /* Here is the pseudo-keyword for declaring pointers that must be "far"
  52.  * on 80x86 machines.  Most of the specialized coding for 80x86 is handled
  53.  * by just saying "FAR *" where such a pointer is needed.  In a few places
  54.  * explicit coding is needed; see uses of the NEED_FAR_POINTERS symbol.
  55.  */
  56.  
  57. #ifdef NEED_FAR_POINTERS
  58. #define FAR  far
  59. #else
  60. #define FAR
  61. #endif
  62.  
  63.  
  64.  
  65. /* The remaining declarations are not system-dependent, we hope. */
  66.  
  67.  
  68. /*
  69.  * NOTE: if you have an ancient, strict-K&R C compiler, it may choke on the
  70.  * similarly-named fields in compress_info_struct and decompress_info_struct.
  71.  * If this happens, you can get around it by rearranging the two structs so
  72.  * that the similarly-named fields appear first and in the same order in
  73.  * each struct.  Since such compilers are now pretty rare, we haven't done
  74.  * this in the portable code, preferring to maintain a logical ordering.
  75.  */
  76.  
  77.  
  78.  
  79. /* This macro is used to declare a "method", that is, a function pointer. */
  80. /* We want to supply prototype parameters if the compiler can cope. */
  81. /* Note that the arglist parameter must be parenthesized! */
  82.  
  83. #ifdef PROTO
  84. #define METHOD(type,methodname,arglist)  type (*methodname) arglist
  85. #else
  86. #define METHOD(type,methodname,arglist)  type (*methodname) ()
  87. #endif
  88.  
  89. /* Forward references to lists of method pointers */
  90. typedef struct external_methods_struct * external_methods_ptr;
  91. typedef struct compress_methods_struct * compress_methods_ptr;
  92. typedef struct decompress_methods_struct * decompress_methods_ptr;
  93.  
  94.  
  95. /* Data structures for images containing either samples or coefficients. */
  96. /* Note that the topmost (leftmost) index is always color component. */
  97. /* On 80x86 machines, the image arrays are too big for near pointers, */
  98. /* but the pointer arrays can fit in near memory. */
  99.  
  100. typedef JSAMPLE FAR *JSAMPROW;  /* ptr to one image row of pixel samples. */
  101. typedef JSAMPROW *JSAMPARRAY;   /* ptr to some rows (a 2-D sample array) */
  102. typedef JSAMPARRAY *JSAMPIMAGE; /* a 3-D sample array: top index is color */
  103.  
  104.  
  105. #define DCTSIZE         8       /* The basic DCT block is 8x8 samples */
  106. #define DCTSIZE2        64      /* DCTSIZE squared; # of elements in a block */
  107.  
  108. typedef JCOEF JBLOCK[DCTSIZE2]; /* one block of coefficients */
  109. typedef JBLOCK FAR *JBLOCKROW;  /* pointer to one row of coefficient blocks */
  110. typedef JBLOCKROW *JBLOCKARRAY;         /* a 2-D array of coefficient blocks */
  111. typedef JBLOCKARRAY *JBLOCKIMAGE;       /* a 3-D array of coefficient blocks */
  112.  
  113. typedef JCOEF FAR *JCOEFPTR;    /* useful in a couple of places */
  114.  
  115.  
  116. /* The input and output data of the DCT transform subroutines are of
  117.  * the following type, which need not be the same as JCOEF.
  118.  * For example, on a machine with fast floating point, it might make sense
  119.  * to recode the DCT routines to use floating point; then DCTELEM would be
  120.  * 'float' or 'double'.
  121.  */
  122.  
  123. typedef JCOEF DCTELEM;
  124. typedef DCTELEM DCTBLOCK[DCTSIZE2];
  125.  
  126.  
  127. /* Types for JPEG compression parameters and working tables. */
  128.  
  129.  
  130. typedef enum {                  /* defines known color spaces */
  131.         CS_UNKNOWN,             /* error/unspecified */
  132.         CS_GRAYSCALE,           /* monochrome (only 1 component) */
  133.         CS_RGB,                 /* red/green/blue */
  134.         CS_YCbCr,               /* Y/Cb/Cr (also known as YUV) */
  135.         CS_YIQ,                 /* Y/I/Q */
  136.         CS_CMYK                 /* C/M/Y/K */
  137. } COLOR_SPACE;
  138.  
  139.  
  140. typedef struct {                /* Basic info about one component */
  141.   /* These values are fixed over the whole image */
  142.   /* For compression, they must be supplied by the user interface; */
  143.   /* for decompression, they are read from the SOF marker. */
  144.         short component_id;     /* identifier for this component (0..255) */
  145.         short component_index;  /* its index in SOF or cinfo->comp_info[] */
  146.         short h_samp_factor;    /* horizontal sampling factor (1..4) */
  147.         short v_samp_factor;    /* vertical sampling factor (1..4) */
  148.         short quant_tbl_no;     /* quantization table selector (0..3) */
  149.   /* These values may vary between scans */
  150.   /* For compression, they must be supplied by the user interface; */
  151.   /* for decompression, they are read from the SOS marker. */
  152.         short dc_tbl_no;        /* DC entropy table selector (0..3) */
  153.         short ac_tbl_no;        /* AC entropy table selector (0..3) */
  154.   /* These values are computed during compression or decompression startup */
  155.         long true_comp_width;   /* component's image width in samples */
  156.         long true_comp_height;  /* component's image height in samples */
  157.         /* the above are the logical dimensions of the subsampled image */
  158.   /* These values are computed before starting a scan of the component */
  159.         short MCU_width;        /* number of blocks per MCU, horizontally */
  160.         short MCU_height;       /* number of blocks per MCU, vertically */
  161.         short MCU_blocks;       /* MCU_width * MCU_height */
  162.         long subsampled_width;  /* image width in samples, after expansion */
  163.         long subsampled_height; /* image height in samples, after expansion */
  164.         /* the above are the true_comp_xxx values rounded up to multiples of */
  165.         /* the MCU dimensions; these are the working dimensions of the array */
  166.         /* as it is passed through the DCT or IDCT step.  NOTE: these values */
  167.         /* differ depending on whether the component is interleaved or not!! */
  168. } jpeg_component_info;
  169.  
  170.  
  171. /* DCT coefficient quantization tables.
  172.  * For 8-bit precision, 'INT16' should be good enough for quantization values;
  173.  * for more precision, we go for the full 16 bits.  'INT16' provides a useful
  174.  * speedup on many machines (multiplication & division of JCOEFs by
  175.  * quantization values is a significant chunk of the runtime).
  176.  * Note: the values in a QUANT_TBL are always given in zigzag order.
  177.  */
  178. #ifdef EIGHT_BIT_SAMPLES
  179. typedef INT16 QUANT_VAL;        /* element of a quantization table */
  180. #else
  181. typedef UINT16 QUANT_VAL;       /* element of a quantization table */
  182. #endif
  183. typedef QUANT_VAL QUANT_TBL[DCTSIZE2];  /* A quantization table */
  184. typedef QUANT_VAL * QUANT_TBL_PTR;      /* pointer to same */
  185.  
  186.  
  187. typedef struct {                /* A Huffman coding table */
  188.   /* These two fields directly represent the contents of a JPEG DHT marker */
  189.         UINT8 bits[17];         /* bits[k] = # of symbols with codes of */
  190.                                 /* length k bits; bits[0] is unused */
  191.         UINT8 huffval[256];     /* The symbols, in order of incr code length */
  192.   /* This field is used only during compression.  It's initialized FALSE when
  193.    * the table is created, and set TRUE when it's been output to the file.
  194.    */
  195.         boolean sent_table;     /* TRUE when table has been output */
  196.   /* The remaining fields are computed from the above to allow more efficient
  197.    * coding and decoding.  These fields should be considered private to the
  198.    * Huffman compression & decompression modules.
  199.    */
  200.         UINT16 ehufco[256];     /* code for each symbol */
  201.         char ehufsi[256];       /* length of code for each symbol */
  202.         UINT16 mincode[17];     /* smallest code of length k */
  203.         INT32 maxcode[17];      /* largest code of length k (-1 if none) */
  204.         short valptr[17];       /* huffval[] index of 1st symbol of length k */
  205. } HUFF_TBL;
  206.  
  207.  
  208. #define NUM_QUANT_TBLS      4   /* quantization tables are numbered 0..3 */
  209. #define NUM_HUFF_TBLS       4   /* Huffman tables are numbered 0..3 */
  210. #define NUM_ARITH_TBLS      16  /* arith-coding tables are numbered 0..15 */
  211. #define MAX_COMPS_IN_SCAN   4   /* JPEG limit on # of components in one scan */
  212. #define MAX_SAMP_FACTOR     4   /* JPEG limit on sampling factors */
  213. #define MAX_BLOCKS_IN_MCU   10  /* JPEG limit on # of blocks in an MCU */
  214.  
  215.  
  216. /* Working data for compression */
  217.  
  218. struct compress_info_struct {
  219. /*
  220.  * All of these fields shall be established by the user interface before
  221.  * calling jpeg_compress, or by the input_init or c_ui_method_selection
  222.  * methods.
  223.  * Most parameters can be set to reasonable defaults by j_default_compression.
  224.  * Note that the UI must supply the storage for the main methods struct,
  225.  * though it sets only a few of the methods there.
  226.  */
  227.         compress_methods_ptr methods; /* Points to list of methods to use */
  228.  
  229.         external_methods_ptr emethods; /* Points to list of methods to use */
  230.  
  231.         IFILEREF input_file;    /* tells input routines where to read image */
  232.         JFILEREF output_file;   /* tells output routines where to write JPEG */
  233.  
  234.         long image_width;       /* input image width */
  235.         long image_height;      /* input image height */
  236.         short input_components; /* # of color components in input image */
  237.  
  238.         short data_precision;   /* bits of precision in image data */
  239.  
  240.         COLOR_SPACE in_color_space; /* colorspace of input file */
  241.         COLOR_SPACE jpeg_color_space; /* colorspace of JPEG file */
  242.  
  243.         double input_gamma;     /* image gamma of input file */
  244.  
  245.         boolean write_JFIF_header; /* should a JFIF marker be written? */
  246.         /* These three values are not used by the JPEG code, only copied */
  247.         /* into the JFIF APP0 marker.  density_unit can be 0 for unknown, */
  248.         /* 1 for dots/inch, or 2 for dots/cm.  Note that the pixel aspect */
  249.         /* ratio is defined by X_density/Y_density even when density_unit=0. */
  250.         UINT8 density_unit;     /* JFIF code for pixel size units */
  251.         UINT16 X_density;       /* Horizontal pixel density */
  252.         UINT16 Y_density;       /* Vertical pixel density */
  253.  
  254.         short num_components;   /* # of color components in JPEG image */
  255.         jpeg_component_info * comp_info;
  256.         /* comp_info[i] describes component that appears i'th in SOF */
  257.  
  258.         QUANT_TBL_PTR quant_tbl_ptrs[NUM_QUANT_TBLS];
  259.         /* ptrs to coefficient quantization tables, or NULL if not defined */
  260.  
  261.         HUFF_TBL * dc_huff_tbl_ptrs[NUM_HUFF_TBLS];
  262.         HUFF_TBL * ac_huff_tbl_ptrs[NUM_HUFF_TBLS];
  263.         /* ptrs to Huffman coding tables, or NULL if not defined */
  264.  
  265.         UINT8 arith_dc_L[NUM_ARITH_TBLS]; /* L values for DC arithmetic-coding tables */
  266.         UINT8 arith_dc_U[NUM_ARITH_TBLS]; /* U values for DC arithmetic-coding tables */
  267.         UINT8 arith_ac_K[NUM_ARITH_TBLS]; /* Kx values for AC arithmetic-coding tables */
  268.  
  269.         boolean arith_code;     /* TRUE=arithmetic coding, FALSE=Huffman */
  270.         boolean interleave;     /* TRUE=interleaved output, FALSE=not */
  271.         boolean optimize_coding; /* TRUE=optimize entropy encoding parms */
  272.         boolean CCIR601_sampling; /* TRUE=first samples are cosited */
  273.  
  274.         UINT16 restart_interval;/* MDUs per restart interval, or 0 for no restart */
  275.  
  276. /*
  277.  * These fields are computed during jpeg_compress startup
  278.  */
  279.         short max_h_samp_factor; /* largest h_samp_factor */
  280.         short max_v_samp_factor; /* largest v_samp_factor */
  281.  
  282. /*
  283.  * These fields are valid during any one scan
  284.  */
  285.         short comps_in_scan;    /* # of JPEG components output this time */
  286.         jpeg_component_info * cur_comp_info[MAX_COMPS_IN_SCAN];
  287.         /* *cur_comp_info[i] describes component that appears i'th in SOS */
  288.  
  289.         long MCUs_per_row;      /* # of MCUs across the image */
  290.         long MCU_rows_in_scan;  /* # of MCU rows in the image */
  291.  
  292.         short blocks_in_MCU;    /* # of DCT blocks per MCU */
  293.         short MCU_membership[MAX_BLOCKS_IN_MCU];
  294.         /* MCU_membership[i] is index in cur_comp_info of component owning */
  295.         /* i'th block in an MCU */
  296.  
  297.         /* these fields are private data for the entropy encoder */
  298.         JCOEF last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each comp */
  299.         JCOEF last_dc_diff[MAX_COMPS_IN_SCAN]; /* last DC diff for each comp */
  300.         UINT16 restarts_to_go;  /* MDUs left in this restart interval */
  301.         short next_restart_num; /* # of next RSTn marker (0..7) */
  302. };
  303.  
  304. typedef struct compress_info_struct * compress_info_ptr;
  305.  
  306.  
  307. /* Working data for decompression */
  308.  
  309. struct decompress_info_struct {
  310. /*
  311.  * These fields shall be established by the user interface before
  312.  * calling jpeg_decompress.  Note that the UI must supply the storage for
  313.  * the main methods struct, though it sets only a few of the methods there.
  314.  */
  315.         decompress_methods_ptr methods; /* Points to list of methods to use */
  316.  
  317.         external_methods_ptr emethods; /* Points to list of methods to use */
  318.  
  319.         JFILEREF input_file;    /* tells input routines where to read JPEG */
  320.         IFILEREF output_file;   /* tells output routines where to write image */
  321.  
  322.         /* these can be set at d_ui_method_selection time: */
  323.  
  324.         COLOR_SPACE out_color_space; /* colorspace of output */
  325.  
  326.         double output_gamma;    /* image gamma wanted in output */
  327.  
  328.         boolean quantize_colors; /* T if output is a colormapped format */
  329.         /* the following are ignored if not quantize_colors: */
  330.         boolean two_pass_quantize;      /* use two-pass color quantization? */
  331.         boolean use_dithering;          /* want color dithering? */
  332.         int desired_number_of_colors;   /* number of colors to use */
  333.  
  334.         boolean do_block_smoothing; /* T = apply cross-block smoothing */
  335.         boolean do_pixel_smoothing; /* T = apply post-subsampling smoothing */
  336.  
  337. /*
  338.  * These fields are used for efficient buffering of data between read_jpeg_data
  339.  * and the entropy decoding object.  By using a shared buffer, we avoid copying
  340.  * data and eliminate the need for an "unget" operation at the end of a scan.
  341.  * The actual source of the data is known only to read_jpeg_data; see the
  342.  * JGETC macro, below.
  343.  * Note: the user interface is expected to allocate the input_buffer and
  344.  * initialize bytes_in_buffer to 0.  Also, for JFIF/raw-JPEG input, the UI
  345.  * actually supplies the read_jpeg_data method.
  346.  */
  347.         char * input_buffer;    /* start of buffer (private to input code) */
  348.         char * next_input_byte; /* => next byte to read from buffer */
  349.         int bytes_in_buffer;    /* # of bytes remaining in buffer */
  350.  
  351. /*
  352.  * These fields are set by read_file_header or read_scan_header
  353.  */
  354.         long image_width;       /* overall image width */
  355.         long image_height;      /* overall image height */
  356.  
  357.         short data_precision;   /* bits of precision in image data */
  358.  
  359.         COLOR_SPACE jpeg_color_space; /* colorspace of JPEG file */
  360.  
  361.         /* These three values are not used by the JPEG code, merely copied */
  362.         /* from the JFIF APP0 marker (if any). */
  363.         UINT8 density_unit;     /* JFIF code for pixel size units */
  364.         UINT16 X_density;       /* Horizontal pixel density */
  365.         UINT16 Y_density;       /* Vertical pixel density */
  366.  
  367.         short num_components;   /* # of color components in JPEG image */
  368.         jpeg_component_info * comp_info;
  369.         /* comp_info[i] describes component that appears i'th in SOF */
  370.  
  371.         QUANT_TBL_PTR quant_tbl_ptrs[NUM_QUANT_TBLS];
  372.         /* ptrs to coefficient quantization tables, or NULL if not defined */
  373.  
  374.         HUFF_TBL * dc_huff_tbl_ptrs[NUM_HUFF_TBLS];
  375.         HUFF_TBL * ac_huff_tbl_ptrs[NUM_HUFF_TBLS];
  376.         /* ptrs to Huffman coding tables, or NULL if not defined */
  377.  
  378.         UINT8 arith_dc_L[NUM_ARITH_TBLS]; /* L values for DC arith-coding tables */
  379.         UINT8 arith_dc_U[NUM_ARITH_TBLS]; /* U values for DC arith-coding tables */
  380.         UINT8 arith_ac_K[NUM_ARITH_TBLS]; /* Kx values for AC arith-coding tables */
  381.  
  382.         boolean arith_code;     /* TRUE=arithmetic coding, FALSE=Huffman */
  383.         boolean CCIR601_sampling; /* TRUE=first samples are cosited */
  384.  
  385.         UINT16 restart_interval;/* MDUs per restart interval, or 0 for no restart */
  386.  
  387. /*
  388.  * These fields are computed during jpeg_decompress startup
  389.  */
  390.         short max_h_samp_factor; /* largest h_samp_factor */
  391.         short max_v_samp_factor; /* largest v_samp_factor */
  392.  
  393.         short color_out_comps;  /* # of color components output by color_convert */
  394.                                 /* (need not match num_components) */
  395.         short final_out_comps;  /* # of color components in output image */
  396.         /* (1 when quantizing colors, else same as color_out_comps) */
  397.  
  398. /*
  399.  * These fields are valid during any one scan
  400.  */
  401.         short comps_in_scan;    /* # of JPEG components input this time */
  402.         jpeg_component_info * cur_comp_info[MAX_COMPS_IN_SCAN];
  403.         /* *cur_comp_info[i] describes component that appears i'th in SOS */
  404.  
  405.         long MCUs_per_row;      /* # of MCUs across the image */
  406.         long MCU_rows_in_scan;  /* # of MCU rows in the image */
  407.  
  408.         short blocks_in_MCU;    /* # of DCT blocks per MCU */
  409.         short MCU_membership[MAX_BLOCKS_IN_MCU];
  410.         /* MCU_membership[i] is index in cur_comp_info of component owning */
  411.         /* i'th block in an MCU */
  412.  
  413.         /* these fields are private data for the entropy encoder */
  414.         JCOEF last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each comp */
  415.         JCOEF last_dc_diff[MAX_COMPS_IN_SCAN]; /* last DC diff for each comp */
  416.         UINT16 restarts_to_go;  /* MDUs left in this restart interval */
  417.         short next_restart_num; /* # of next RSTn marker (0..7) */
  418. };
  419.  
  420. typedef struct decompress_info_struct * decompress_info_ptr;
  421.  
  422.  
  423. /* Macros for reading data from the decompression input buffer */
  424.  
  425. #ifdef CHAR_IS_UNSIGNED
  426. #define JGETC(cinfo)    ( --(cinfo)->bytes_in_buffer < 0 ? \
  427.                          (*(cinfo)->methods->read_jpeg_data) (cinfo) : \
  428.                          (int) *(cinfo)->next_input_byte++ )
  429. #else
  430. #define JGETC(cinfo)    ( --(cinfo)->bytes_in_buffer < 0 ? \
  431.                          (*(cinfo)->methods->read_jpeg_data) (cinfo) : \
  432.                          (int) (*(cinfo)->next_input_byte++) & 0xFF )
  433. #endif
  434.  
  435. #define JUNGETC(ch,cinfo)  ((cinfo)->bytes_in_buffer++, \
  436.                             *(--((cinfo)->next_input_byte)) = (ch))
  437.  
  438. #define MIN_UNGET       2       /* may always do at least 2 JUNGETCs */
  439.  
  440.  
  441. /* A virtual image has a control block whose contents are private to the
  442.  * memory manager module (and may differ between managers).  The rest of the
  443.  * code only refers to virtual images by these pointer types.
  444.  */
  445.  
  446. typedef struct big_sarray_control * big_sarray_ptr;
  447. typedef struct big_barray_control * big_barray_ptr;
  448.  
  449.  
  450. /* Method types that need typedefs */
  451.  
  452. typedef METHOD(void, MCU_output_method_ptr, (compress_info_ptr cinfo,
  453.                                              JBLOCK *MCU_data));
  454. typedef METHOD(void, MCU_output_caller_ptr, (compress_info_ptr cinfo,
  455.                                              MCU_output_method_ptr output_method));
  456. typedef METHOD(void, subsample_ptr, (compress_info_ptr cinfo,
  457.                                      int which_component,
  458.                                      long input_cols, int input_rows,
  459.                                      long output_cols, int output_rows,
  460.                                      JSAMPARRAY above,
  461.                                      JSAMPARRAY input_data,
  462.                                      JSAMPARRAY below,
  463.                                      JSAMPARRAY output_data));
  464. typedef METHOD(void, unsubsample_ptr, (decompress_info_ptr cinfo,
  465.                                        int which_component,
  466.                                        long input_cols, int input_rows,
  467.                                        long output_cols, int output_rows,
  468.                                        JSAMPARRAY above,
  469.                                        JSAMPARRAY input_data,
  470.                                        JSAMPARRAY below,
  471.                                        JSAMPARRAY output_data));
  472. typedef METHOD(void, quantize_method_ptr, (decompress_info_ptr cinfo,
  473.                                            int num_rows,
  474.                                            JSAMPIMAGE input_data,
  475.                                            JSAMPARRAY output_workspace));
  476. typedef METHOD(void, quantize_caller_ptr, (decompress_info_ptr cinfo,
  477.                                            quantize_method_ptr quantize_method));
  478.  
  479.  
  480. /* These structs contain function pointers for the various JPEG methods. */
  481.  
  482. /* Routines to be provided by the surrounding application, rather than the
  483.  * portable JPEG code proper.  These are the same for compression and
  484.  * decompression.
  485.  */
  486.  
  487. struct external_methods_struct {
  488.         /* User interface: error exit and trace message routines */
  489.         /* NOTE: the string msgtext parameters will eventually be replaced */
  490.         /* by an enumerated-type code so that non-English error messages */
  491.         /* can be substituted easily.  This will not be done until all the */
  492.         /* code is in place, so that we know what messages are needed. */
  493.         METHOD(void, error_exit, (char *msgtext));
  494.         METHOD(void, trace_message, (char *msgtext));
  495.  
  496.         /* Working data for error/trace facility */
  497.         /* See macros below for the usage of these variables */
  498.         int trace_level;        /* level of detail of tracing messages */
  499.         /* Use level 0 for unsuppressable messages (nonfatal errors) */
  500.         /* Use levels 1, 2, 3 for successively more detailed trace options */
  501.  
  502.         int message_parm[8];    /* store numeric parms for messages here */
  503.  
  504.         /* Memory management */
  505.         /* NB: alloc routines never return NULL. They exit to */
  506.         /* error_exit if not successful. */
  507.         METHOD(void *, alloc_small, (size_t sizeofobject));
  508.         METHOD(void, free_small, (void *ptr));
  509. #ifdef NEED_FAR_POINTERS        /* routines for getting far-heap space */
  510.         METHOD(void FAR *, alloc_medium, (size_t sizeofobject));
  511.         METHOD(void, free_medium, (void FAR *ptr));
  512. #else
  513. #define alloc_medium alloc_small
  514. #define free_medium  free_small
  515. #endif
  516.         METHOD(JSAMPARRAY, alloc_small_sarray, (long samplesperrow,
  517.                                                 long numrows));
  518.         METHOD(void, free_small_sarray, (JSAMPARRAY ptr,
  519.                                          long numrows));
  520.         METHOD(JBLOCKARRAY, alloc_small_barray, (long blocksperrow,
  521.                                                  long numrows));
  522.         METHOD(void, free_small_barray, (JBLOCKARRAY ptr,
  523.                                          long numrows));
  524.         METHOD(big_sarray_ptr, request_big_sarray, (long samplesperrow,
  525.                                                     long numrows,
  526.                                                     long unitheight));
  527.         METHOD(big_barray_ptr, request_big_barray, (long blocksperrow,
  528.                                                     long numrows,
  529.                                                     long unitheight));
  530.         METHOD(void, alloc_big_arrays, (long extra_small_samples,
  531.                                         long extra_small_blocks,
  532.                                         long extra_medium_space));
  533.         METHOD(JSAMPARRAY, access_big_sarray, (big_sarray_ptr ptr,
  534.                                                long start_row,
  535.                                                boolean writable));
  536.         METHOD(JBLOCKARRAY, access_big_barray, (big_barray_ptr ptr,
  537.                                                 long start_row,
  538.                                                 boolean writable));
  539.         METHOD(void, free_big_sarray, (big_sarray_ptr ptr));
  540.         METHOD(void, free_big_barray, (big_barray_ptr ptr));
  541. };
  542.  
  543. /* Macros to simplify using the error and trace message stuff */
  544. /* The first parameter is generally cinfo->emethods */
  545.  
  546. #define ERREXIT(emeth,msg)              ((*(emeth)->error_exit) (msg))
  547. #define ERREXIT1(emeth,msg,p1)          ((emeth)->message_parm[0] = (p1), \
  548.                                          (*(emeth)->error_exit) (msg))
  549. #define ERREXIT2(emeth,msg,p1,p2)       ((emeth)->message_parm[0] = (p1), \
  550.                                          (emeth)->message_parm[1] = (p2), \
  551.                                          (*(emeth)->error_exit) (msg))
  552. #define ERREXIT3(emeth,msg,p1,p2,p3)    ((emeth)->message_parm[0] = (p1), \
  553.                                          (emeth)->message_parm[1] = (p2), \
  554.                                          (emeth)->message_parm[2] = (p3), \
  555.                                          (*(emeth)->error_exit) (msg))
  556. #define ERREXIT4(emeth,msg,p1,p2,p3,p4) ((emeth)->message_parm[0] = (p1), \
  557.                                          (emeth)->message_parm[1] = (p2), \
  558.                                          (emeth)->message_parm[2] = (p3), \
  559.                                          (emeth)->message_parm[3] = (p4), \
  560.                                          (*(emeth)->error_exit) (msg))
  561.  
  562. #define TRACEMS(emeth,lvl,msg)    \
  563.   ( (emeth)->trace_level >= (lvl) ? \
  564.    ((*(emeth)->trace_message) (msg), 0) : 0)
  565. #define TRACEMS1(emeth,lvl,msg,p1)    \
  566.   ( (emeth)->trace_level >= (lvl) ? \
  567.    ((emeth)->message_parm[0] = (p1), \
  568.     (*(emeth)->trace_message) (msg), 0) : 0)
  569. #define TRACEMS2(emeth,lvl,msg,p1,p2)    \
  570.   ( (emeth)->trace_level >= (lvl) ? \
  571.    ((emeth)->message_parm[0] = (p1), \
  572.     (emeth)->message_parm[1] = (p2), \
  573.     (*(emeth)->trace_message) (msg), 0) : 0)
  574. #define TRACEMS3(emeth,lvl,msg,p1,p2,p3)    \
  575.   ( (emeth)->trace_level >= (lvl) ? \
  576.    ((emeth)->message_parm[0] = (p1), \
  577.     (emeth)->message_parm[1] = (p2), \
  578.     (emeth)->message_parm[2] = (p3), \
  579.     (*(emeth)->trace_message) (msg), 0) : 0)
  580. #define TRACEMS4(emeth,lvl,msg,p1,p2,p3,p4)    \
  581.   ( (emeth)->trace_level >= (lvl) ? \
  582.    ((emeth)->message_parm[0] = (p1), \
  583.     (emeth)->message_parm[1] = (p2), \
  584.     (emeth)->message_parm[2] = (p3), \
  585.     (emeth)->message_parm[3] = (p4), \
  586.     (*(emeth)->trace_message) (msg), 0) : 0)
  587. #define TRACEMS8(emeth,lvl,msg,p1,p2,p3,p4,p5,p6,p7,p8)    \
  588.   ( (emeth)->trace_level >= (lvl) ? \
  589.    ((emeth)->message_parm[0] = (p1), \
  590.     (emeth)->message_parm[1] = (p2), \
  591.     (emeth)->message_parm[2] = (p3), \
  592.     (emeth)->message_parm[3] = (p4), \
  593.     (emeth)->message_parm[4] = (p5), \
  594.     (emeth)->message_parm[5] = (p6), \
  595.     (emeth)->message_parm[6] = (p7), \
  596.     (emeth)->message_parm[7] = (p8), \
  597.     (*(emeth)->trace_message) (msg), 0) : 0)
  598.  
  599.  
  600. /* Methods used during JPEG compression. */
  601.  
  602. struct compress_methods_struct {
  603.         /* Hook for user interface to get control after input_init */
  604.         METHOD(void, c_ui_method_selection, (compress_info_ptr cinfo));
  605.         /* Input image reading & conversion to standard form */
  606.         METHOD(void, input_init, (compress_info_ptr cinfo));
  607.         METHOD(void, get_input_row, (compress_info_ptr cinfo,
  608.                                      JSAMPARRAY pixel_row));
  609.         METHOD(void, input_term, (compress_info_ptr cinfo));
  610.         /* Gamma and color space conversion */
  611.         METHOD(void, colorin_init, (compress_info_ptr cinfo));
  612.         METHOD(void, get_sample_rows, (compress_info_ptr cinfo,
  613.                                        int rows_to_read,
  614.                                        JSAMPIMAGE image_data));
  615.         METHOD(void, colorin_term, (compress_info_ptr cinfo));
  616.         /* Expand picture data at edges */
  617.         METHOD(void, edge_expand, (compress_info_ptr cinfo,
  618.                                    long input_cols, int input_rows,
  619.                                    long output_cols, int output_rows,
  620.                                    JSAMPIMAGE image_data));
  621.         /* Subsample pixel values of a single component */
  622.         /* There can be a different subsample method for each component */
  623.         METHOD(void, subsample_init, (compress_info_ptr cinfo));
  624.         subsample_ptr subsample[MAX_COMPS_IN_SCAN];
  625.         METHOD(void, subsample_term, (compress_info_ptr cinfo));
  626.         /* Extract samples in MCU order, process & hand off to output_method */
  627.         /* The input is always exactly N MCU rows worth of data */
  628.         METHOD(void, extract_init, (compress_info_ptr cinfo));
  629.         METHOD(void, extract_MCUs, (compress_info_ptr cinfo,
  630.                                     JSAMPIMAGE image_data,
  631.                                     int num_mcu_rows,
  632.                                     MCU_output_method_ptr output_method));
  633.         METHOD(void, extract_term, (compress_info_ptr cinfo));
  634.         /* Entropy encoding parameter optimization */
  635.         METHOD(void, entropy_optimize, (compress_info_ptr cinfo,
  636.                                         MCU_output_caller_ptr source_method));
  637.         /* Entropy encoding */
  638.         METHOD(void, entropy_encoder_init, (compress_info_ptr cinfo));
  639.         METHOD(void, entropy_encode, (compress_info_ptr cinfo,
  640.                                       JBLOCK *MCU_data));
  641.         METHOD(void, entropy_encoder_term, (compress_info_ptr cinfo));
  642.         /* JPEG file header construction */
  643.         METHOD(void, write_file_header, (compress_info_ptr cinfo));
  644.         METHOD(void, write_scan_header, (compress_info_ptr cinfo));
  645.         METHOD(void, write_jpeg_data, (compress_info_ptr cinfo,
  646.                                        char *dataptr,
  647.                                        int datacount));
  648.         METHOD(void, write_scan_trailer, (compress_info_ptr cinfo));
  649.         METHOD(void, write_file_trailer, (compress_info_ptr cinfo));
  650.         /* Pipeline control */
  651.         METHOD(void, c_pipeline_controller, (compress_info_ptr cinfo));
  652.         METHOD(void, entropy_output, (compress_info_ptr cinfo,
  653.                                       char *dataptr,
  654.                                       int datacount));
  655.         /* Overall control */
  656.         METHOD(void, c_per_scan_method_selection, (compress_info_ptr cinfo));
  657. };
  658.  
  659. /* Methods used during JPEG decompression. */
  660.  
  661. struct decompress_methods_struct {
  662.         /* Hook for user interface to get control after reading file header */
  663.         METHOD(void, d_ui_method_selection, (decompress_info_ptr cinfo));
  664.         /* JPEG file scanning */
  665.         /* Note: user interface supplies read_jpeg_data for JFIF/raw-JPEG
  666.          * reading.  For file formats that require random access (eg, TIFF)
  667.          * the JPEG file header module will override the UI read_jpeg_data.
  668.          */
  669.         METHOD(void, read_file_header, (decompress_info_ptr cinfo));
  670.         METHOD(boolean, read_scan_header, (decompress_info_ptr cinfo));
  671.         METHOD(int, read_jpeg_data, (decompress_info_ptr cinfo));
  672.         METHOD(void, read_scan_trailer, (decompress_info_ptr cinfo));
  673.         METHOD(void, read_file_trailer, (decompress_info_ptr cinfo));
  674.         /* Entropy decoding */
  675.         METHOD(void, entropy_decoder_init, (decompress_info_ptr cinfo));
  676.         METHOD(void, entropy_decode, (decompress_info_ptr cinfo,
  677.                                       JBLOCK *MCU_data));
  678.         METHOD(void, entropy_decoder_term, (decompress_info_ptr cinfo));
  679.         /* MCU disassembly: fetch MCUs from entropy_decode, build coef array */
  680.         METHOD(void, disassemble_init, (decompress_info_ptr cinfo));
  681.         METHOD(void, disassemble_MCU, (decompress_info_ptr cinfo,
  682.                                        JBLOCKIMAGE image_data));
  683.         METHOD(void, disassemble_term, (decompress_info_ptr cinfo));
  684.         /* Cross-block smoothing */
  685.         METHOD(void, smooth_coefficients, (decompress_info_ptr cinfo,
  686.                                            jpeg_component_info *compptr,
  687.                                            JBLOCKROW above,
  688.                                            JBLOCKROW currow,
  689.                                            JBLOCKROW below,
  690.                                            JBLOCKROW output));
  691.         /* Un-subsample pixel values of a single component */
  692.         /* There can be a different unsubsample method for each component */
  693.         METHOD(void, unsubsample_init, (decompress_info_ptr cinfo));
  694.         unsubsample_ptr unsubsample[MAX_COMPS_IN_SCAN];
  695.         METHOD(void, unsubsample_term, (decompress_info_ptr cinfo));
  696.         /* Gamma and color space conversion */
  697.         METHOD(void, colorout_init, (decompress_info_ptr cinfo));
  698.         METHOD(void, color_convert, (decompress_info_ptr cinfo,
  699.                                      int num_rows,
  700.                                      JSAMPIMAGE input_data,
  701.                                      JSAMPIMAGE output_data));
  702.         METHOD(void, colorout_term, (decompress_info_ptr cinfo));
  703.         /* Color quantization */
  704.         METHOD(void, color_quant_init, (decompress_info_ptr cinfo));
  705.         METHOD(void, color_quantize, (decompress_info_ptr cinfo,
  706.                                       int num_rows,
  707.                                       JSAMPIMAGE input_data,
  708.                                       JSAMPARRAY output_data));
  709.         METHOD(void, color_quant_prescan, (decompress_info_ptr cinfo,
  710.                                            int num_rows,
  711.                                            JSAMPIMAGE image_data));
  712.         METHOD(void, color_quant_doit, (decompress_info_ptr cinfo,
  713.                                         quantize_caller_ptr source_method));
  714.         METHOD(void, color_quant_term, (decompress_info_ptr cinfo));
  715.         /* Output image writing */
  716.         METHOD(void, output_init, (decompress_info_ptr cinfo));
  717.         METHOD(void, put_color_map, (decompress_info_ptr cinfo,
  718.                                      int num_colors, JSAMPARRAY colormap));
  719.         METHOD(void, put_pixel_rows, (decompress_info_ptr cinfo,
  720.                                       int num_rows,
  721.                                       JSAMPIMAGE pixel_data));
  722.         METHOD(void, output_term, (decompress_info_ptr cinfo));
  723.         /* Pipeline control */
  724.         METHOD(void, d_pipeline_controller, (decompress_info_ptr cinfo));
  725.         /* Overall control */
  726.         METHOD(void, d_per_scan_method_selection, (decompress_info_ptr cinfo));
  727. };
  728.  
  729.  
  730. /* External declarations for routines that aren't called via method ptrs. */
  731. /* Note: use "j" as first char of names to minimize namespace pollution. */
  732. /* The PP macro hides prototype parameters from compilers that can't cope. */
  733.  
  734. #ifdef PROTO
  735. #define PP(arglist)     arglist
  736. #else
  737. #define PP(arglist)     ()
  738. #endif
  739.  
  740.  
  741. /* main entry for compression */
  742. EXTERN void jpeg_compress PP((compress_info_ptr cinfo));
  743. /* default parameter setup for compression */
  744. EXTERN void j_default_compression PP((compress_info_ptr cinfo, int quality));
  745. EXTERN void j_monochrome_default PP((compress_info_ptr cinfo));
  746. EXTERN void j_set_quality PP((compress_info_ptr cinfo, int quality,
  747.                               boolean force_baseline));
  748. EXTERN void j_free_defaults PP((compress_info_ptr cinfo));
  749.  
  750. /* main entry for decompression */
  751. EXTERN void jpeg_decompress PP((decompress_info_ptr cinfo));
  752.  
  753. /* forward DCT */
  754. EXTERN void j_fwd_dct PP((DCTBLOCK data));
  755. /* inverse DCT */
  756. EXTERN void j_rev_dct PP((DCTBLOCK data));
  757.  
  758. /* utility routines in jutils.c */
  759. EXTERN long jround_up PP((long a, long b));
  760. EXTERN void jcopy_sample_rows PP((JSAMPARRAY input_array, int source_row,
  761.                                   JSAMPARRAY output_array, int dest_row,
  762.                                   int num_rows, long num_cols));
  763. EXTERN void jcopy_block_row PP((JBLOCKROW input_row, JBLOCKROW output_row,
  764.                                 long num_blocks));
  765. EXTERN void jzero_far PP((void FAR * target, size_t bytestozero));
  766.  
  767. /* method selection routines for compression modules */
  768. EXTERN void jselcpipeline PP((compress_info_ptr cinfo)); /* jcpipe.c */
  769. EXTERN void jselchuffman PP((compress_info_ptr cinfo)); /* jchuff.c */
  770. EXTERN void jselcarithmetic PP((compress_info_ptr cinfo)); /* jcarith.c */
  771. EXTERN void jselexpand PP((compress_info_ptr cinfo)); /* jcexpand.c */
  772. EXTERN void jselsubsample PP((compress_info_ptr cinfo)); /* jcsample.c */
  773. EXTERN void jselcmcu PP((compress_info_ptr cinfo)); /* jcmcu.c */
  774. EXTERN void jselccolor PP((compress_info_ptr cinfo));   /* jccolor.c */
  775. /* The user interface should call one of these to select input format: */
  776. EXTERN void jselrgif PP((compress_info_ptr cinfo)); /* jrdgif.c */
  777. EXTERN void jselrppm PP((compress_info_ptr cinfo)); /* jrdppm.c */
  778. /* and one of these to select output header format: */
  779. EXTERN void jselwjfif PP((compress_info_ptr cinfo)); /* jwrjfif.c */
  780.  
  781. /* method selection routines for decompression modules */
  782. EXTERN void jseldpipeline PP((decompress_info_ptr cinfo)); /* jdpipe.c */
  783. EXTERN void jseldhuffman PP((decompress_info_ptr cinfo)); /* jdhuff.c */
  784. EXTERN void jseldarithmetic PP((decompress_info_ptr cinfo)); /* jdarith.c */
  785. EXTERN void jseldmcu PP((decompress_info_ptr cinfo)); /* jdmcu.c */
  786. EXTERN void jselbsmooth PP((decompress_info_ptr cinfo)); /* jbsmooth.c */
  787. EXTERN void jselunsubsample PP((decompress_info_ptr cinfo)); /* jdsample.c */
  788. EXTERN void jseldcolor PP((decompress_info_ptr cinfo)); /* jdcolor.c */
  789. EXTERN void jsel1quantize PP((decompress_info_ptr cinfo)); /* jquant1.c */
  790. EXTERN void jsel2quantize PP((decompress_info_ptr cinfo)); /* jquant2.c */
  791. /* The user interface should call one of these to select input format: */
  792. EXTERN void jselrjfif PP((decompress_info_ptr cinfo)); /* jrdjfif.c */
  793. /* and one of these to select output image format: */
  794. EXTERN void jselwgif PP((decompress_info_ptr cinfo)); /* jwrgif.c */
  795. EXTERN void jselwppm PP((decompress_info_ptr cinfo)); /* jwrppm.c */
  796.  
  797. /* method selection routines for system-dependent modules */
  798. EXTERN void jselerror PP((external_methods_ptr emethods)); /* jerror.c */
  799. EXTERN void jselvirtmem PP((external_methods_ptr emethods)); /* jvirtmem.c */
  800.  
  801. /* debugging hook in jvirtmem.c */
  802. #ifdef MEM_STATS
  803. EXTERN void j_mem_stats PP((void));
  804. #endif
  805.  
  806. /* Miscellaneous useful macros */
  807.  
  808. #define MAX(a,b)        ((a) > (b) ? (a) : (b))
  809. #define MIN(a,b)        ((a) < (b) ? (a) : (b))
  810.  
  811.  
  812. #define RST0    0xD0            /* RST0 marker code */
  813.